package com.jader.activities;
/*评价详情页*/
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;

import me.maxwin.view.XListView;
import me.maxwin.view.XListView.IXListViewListener;

import com.android.volley.RequestQueue;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jader.adapter.GoodsEvaluteAdapter;
import com.jader.beans.Goods_comment;
import com.jader.util.JaderApplication;
import com.zlp.jader.R;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.app.Activity;
import android.content.Intent;

public class Evalute extends Activity implements IXListViewListener {

	private XListView evalute_list;
	private ImageView backToSeller;
	private List<Goods_comment> goods_comments = new ArrayList<Goods_comment>(0);
	private GoodsEvaluteAdapter goodsEvaluteAdapter;
	private RequestQueue queue;
	private JsonArrayRequest arrayRequest;
	private JaderApplication myApplication;
	private String url = JaderApplication.getIP() + "/Jader/client?type="
			+ JaderApplication.SHOW_COMMENT + "&goodsid=";
	private int goodsid, result, page = 0;
	private String time;
	private MenuItem menuItem;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_evalute);
		initViews();
	}

	private void initViews() {
		evalute_list = (XListView) findViewById(R.id.evalute_list);
		backToSeller = (ImageView) findViewById(R.id.seller_evalute_back);

		evalute_list.setPullLoadEnable(true);
		evalute_list.setXListViewListener(this);

		myApplication = (JaderApplication) getApplication();
		queue = myApplication.getQueue();
		goodsid = getIntent().getIntExtra("goodsid", 0);
		backToSeller.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Evalute.this.finish();
			}
		});
		goodsEvaluteAdapter = new GoodsEvaluteAdapter(goods_comments, this);
		evalute_list.setAdapter(goodsEvaluteAdapter);
		page = goods_comments.size();
		sendRequest(url + goodsid + "&page=" + page);
	}

	private void sendRequest(String url) {
		arrayRequest = new JsonArrayRequest(url, new Listener<JSONArray>() {

			@Override
			public void onResponse(JSONArray response) {
				onLoad();
				Gson gson = new Gson();
				// TypeToken内的泛型就是Json数据中的类型
				Type listType = new TypeToken<ArrayList<Goods_comment>>() {
				}.getType();
				List<Goods_comment> arrayList = gson.fromJson(
						response.toString(), listType);
				for (Goods_comment goods_comment : arrayList) {
					boolean flag = true;
					for (Goods_comment goods_comment2 : goods_comments) {
						if (goods_comment.equals(goods_comment2)) {
							flag = false;
						}
					}
					if (flag) {
						goods_comments.add(result, goods_comment);
						result++;
					}
				}
				goodsEvaluteAdapter.notifyDataSetChanged();
			}
		}, new ErrorListener() {

			@Override
			public void onErrorResponse(VolleyError error) {
				onLoad();
			}
		});
		queue.add(arrayRequest);
	}

	@Override
	public void onRefresh() {
		result = 0;
		page = 0;
		sendRequest(url + goodsid + "&page=" + page);
	}

	@Override
	public void onLoadMore() {
		result = goods_comments.size();
		page = goods_comments.size();
		sendRequest(url + goodsid + "&page=" + page);

	}

	private void onLoad() {
		evalute_list.stopRefresh();
		evalute_list.stopLoadMore();
		if (time == null) {
			evalute_list.setRefreshTime("刚刚");
		} else {
			evalute_list.setRefreshTime(time);
		}
		Date date = new Date();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		time = df.format(date);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.store, menu);
		menuItem = menu.findItem(R.id.action_exits);
		menuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {

			@Override
			public boolean onMenuItemClick(MenuItem arg0) {
				Intent intent = new Intent(Intent.ACTION_MAIN);
				intent.addCategory(Intent.CATEGORY_HOME);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
				startActivity(intent);
				finish();
				return false;
			}
		});
		return true;
	}
}
